package com.xds.gulimall.cart.controller;

import com.xds.common.constant.AuthServerConstant;
import com.xds.gulimall.cart.interceptor.CartInterceptor;
import com.xds.gulimall.cart.service.CatService;
import com.xds.gulimall.cart.to.UserInfoTo;
import com.xds.gulimall.cart.vo.CartItemVo;
import com.xds.gulimall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;


/**
 * @author : baixu
 * @date : 16:52 2021/12/22
 */
@Controller
public class CartController {
    @Autowired
    CatService catService;

    //跳转到购物车
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
//        UserInfoTo userInfoTo = CartInterceptor.toThreadLocal.get();
//        System.out.println(userInfoTo);
        CartVo cartVo = catService.getCart();
        model.addAttribute("cart", cartVo);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     *
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        catService.addToCart(skuId, num);
//    model.addAttribute("skuId",skuId);
//    model.addAttribute("item",cartItem);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.xiaowenplus.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        //重定向到成功页面 再次查询购物车数据即可
        CartItemVo cartItem = catService.getCartItem(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        catService.checkItem(skuId,check);
        return "redirect:http://cart.xiaowenplus.com/cart.html";
    }
    @GetMapping(value = "/countItem")
    public String countItem(@RequestParam(value = "skuId") Long skuId,
                            @RequestParam(value = "num") Integer num) {

        catService.changeItemCount(skuId,num);

        return "redirect:http://cart.xiaowenplus.com/cart.html";
    }
    /**
     * 删除商品信息
     * @param skuId
     * @return
     */
    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam("skuId") Integer skuId) {

        catService.deleteIdCartInfo(skuId);

        return "redirect:http://cart.xiaowenplus.com/cart.html";

    }
    @ResponseBody
    @GetMapping("/currentUserItems")
    public List<CartItemVo> getCurrentItems(){
     return  catService.getUserItems();

    }
}
